﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConferenceArranger.ConferenceComponent;
using ConferenceArranger.BusinessRules;


namespace ConferenceArranger.Strategy
{
        /// <summary>
        /// given a list of talks from TalkProcessor
        /// call SessionScheduler, arrange talks for each session
        /// pass a list of sessions (with talks arranged) to TrackScheduler
        /// </summary>
        class SessionProcessor
        {
            //input
            List<Talk> talkList;

            //output
            List<Session> sessionList;

            //keep track of next session type (Morning and Afternoon used interchangeably)
            string nextSessionType;

            public SessionProcessor(List<Talk> talkList)
            {
                this.talkList = talkList;
                this.nextSessionType = SessionHelper.GetNextSessionType();
                sessionList = new List<Session>();
                
            }

            /// <summary>
            /// arrange talks for each session until there is no talk left(except a dummy one)
            /// </summary>
            public void ArrangeSessions()
            {
                while (talkList.Count > 0)
                {   
                    // no session left except the dummy one
                    if (talkList.Count == 1 && talkList[0].GetInterval() == 0)
                        return;

                    Session newSession = new Session(nextSessionType);
                    // session scheduler helps to select the right talks
                    SessionScheduler sessionScheduler = new SessionScheduler(newSession.GetInterval(), talkList);
                    sessionScheduler.SelectTalk();
                    newSession.SetTalkList(sessionScheduler.GetChosenTalks());
                    newSession.SetStartTimeForEvent();

                    //add the session which has finished selecting talks
                    sessionList.Add(newSession);

                    //update for next iteration, unused talks will be considered for next sessions
                    talkList = sessionScheduler.GetReturnedTalks();
                    UpdateNextSessionType();
                }
            }


            public void UpdateNextSessionType()
            {
                string newSessionType = SessionHelper.GetNextSessionType(nextSessionType);
                nextSessionType = newSessionType;
            }

            public List<Session> GetOutput()
            {
                return sessionList;
            }

        }
    
}
